# Contributor: Dennis Krupenik <dennis@krupenik.com>
# Maintainer: Dennis Krupenik <dennis@krupenik.com>
pkgname=openvino
pkgver=2020.4
_gflags=46f73f88b18aee341538c0dfc22b1710a6abedef
_ade=cbe2db61a659c2cc304c3837406f95c39dfa938e
_googletest=9bd163b993459b2ca6ba2dc508577bbc8774c851
_onednn=2706f56ebab54415be48add2751072065f4b52ab
pkgrel=3
pkgdesc="Deep Learning Deployment Toolkit"
url="https://github.com/openvinotoolkit/openvino"
# limited by geos -> gdal -> vtk -> opencv
# x86: error: '_mm_insert_epi64' was not declared in this scope; did you mean '_mm_insert_epi32'?
# 'usr/openvino/deployment_tools/inference_engine/lib/*/cache.json' not found for others than x86_64
arch="x86_64"
license="Apache-2.0"
depends_dev="opencl-headers opencl-icd-loader-dev opencv-dev"
makedepends="autoconf automake cmake cython gcompat git libtool python3-dev
	py3-setuptools samurai $depends_dev"
subpackages="$pkgname-static py3-$pkgname:py3 $pkgname-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/openvinotoolkit/openvino/archive/$pkgver.tar.gz
	gflags-$_gflags.tar.gz::https://github.com/gflags/gflags/archive/$_gflags.tar.gz
	ade-$_ade.tar.gz::https://github.com/opencv/ade/archive/$_ade.tar.gz
	googletest-$_googletest.tar.gz::https://github.com/openvinotoolkit/googletest/archive/$_googletest.tar.gz
	oneDNN-$_onednn.tar.gz::https://github.com/openvinotoolkit/oneDNN/archive/$_onednn.tar.gz
	cmake-config.patch
	pessimizing-move.patch
	deprecated-copy.patch
	cmake-dont-force-build-type.patch
	include-stdexcept.patch
	fix-missing-include.patch
	"

prepare() {
	# FIXME: use system versions
	cp -r "$srcdir"/ade-*/* inference-engine/thirdparty/ade/
	cp -r "$srcdir"/gflags-*/* inference-engine/samples/thirdparty/gflags/
	cp -r "$srcdir"/googletest-*/* inference-engine/tests/ie_test_utils/common_test_utils/gtest/
	cp -r "$srcdir"/oneDNN-*/* inference-engine/thirdparty/mkl-dnn/

	default_prepare

	sed -e 's|-Werror||g' -i cmake/os_flags.cmake
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-Wno-dev \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr/openvino \
		-DENABLE_ALTERNATIVE_TEMP=OFF \
		-DENABLE_CLANG_FORMAT=OFF \
		-DENABLE_CPPLINT=OFF \
		-DENABLE_GNA=OFF \
		-DENABLE_MYRIAD=OFF \
		-DENABLE_OPENCV=OFF \
		-DENABLE_PROFILING_ITT=OFF \
		-DENABLE_PYTHON=ON \
		-DENABLE_SAMPLES=OFF \
		-DENABLE_SPEECH_DEMO=OFF \
		-DENABLE_VPU=OFF \
		-DNGRAPH_DYNAMIC_COMPONENTS_ENABLE=OFF \
		-DNGRAPH_EXPORT_TARGETS_ENABLE=OFF \
		-DNGRAPH_IE_ENABLE=ON \
		-DNGRAPH_JSON_ENABLE=OFF \
		-DNGRAPH_ONNX_IMPORT_ENABLE=OFF \
		-DNGRAPH_PYTHON_BUILD_ENABLE=OFF \
		-DNGRAPH_TEST_UTIL_ENABLE=OFF \
		-DNGRAPH_UNIT_TEST_ENABLE=OFF \
		-DOpenCV_DIR=/usr/lib/cmake/opencv4 \
		-DNGRAPH_WARNINGS_AS_ERRORS=False \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS -Wno-error" \
		-DTHREADING=SEQ \
		$CMAKE_CROSSOPTS .
	cmake --build build
}

check() {
	CTEST_OUTPUT_ON_FAILURE=1 ninja -C build test
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	cd "$pkgdir"
	mkdir -p usr/bin usr/include usr/lib usr/share/openvino

	mv usr/openvino/include/* usr/include/
	mv usr/openvino/deployment_tools/inference_engine/include/* usr/include/

	# debug
	find usr/openvino -type f

	mv usr/openvino/deployment_tools/inference_engine/lib/*/compile_tool usr/bin/
	mv usr/openvino/deployment_tools/inference_engine/lib/*/*.so usr/lib/
	mv usr/openvino/deployment_tools/inference_engine/lib/*/cache.json usr/share/openvino
	mv usr/openvino/deployment_tools/inference_engine/lib/*/plugins.xml usr/share/openvino

	mv usr/openvino/lib*/* usr/lib/
	mv usr/openvino/python/* usr/lib/

	mv usr/openvino/share/* usr/share/
	mv usr/openvino/deployment_tools/inference_engine/share/* usr/share/

	rm -rf usr/openvino

	# These are part of third-party packages and should not be included
	rm -rf usr/include/pugiconfig.hpp usr/include/pugixml.hpp
	rm -rf usr/include/clDNN usr/include/cldnn
	rm -rf usr/include/ade
	rm -rf usr/lib/cmake/ade
	rm -rf usr/lib/cmake/pugixml
	rm -rf usr/lib/pkgconfig/gflags.pc
}

dev() {
	default_dev

	mkdir -p "$subpkgdir"/usr/lib/cmake/InferenceEngine
	mv "$pkgdir"/usr/share/InferenceEngine*.cmake "$subpkgdir"/usr/lib/cmake/InferenceEngine/
	mv "$pkgdir"/usr/share/ie_parallel.cmake "$subpkgdir"/usr/lib/cmake/InferenceEngine/
}

py3() {
	pkgdesc="$pkgname python bindings"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/python* "$subpkgdir"/usr/lib/
}

sha512sums="
67cfe44f587f1b5a96cac3d0dbfbf299e09d47afef626d2a56e0ea8fbe7c24a06bc1ae8fb2c3c3f634637f34826b5a9eb53190697587f966b4140b8400061a6f  openvino-2020.4.tar.gz
cf4ba466421959b4be24efa9304f32d831a24e57b6a88d1806b232bf564a67d63a1012c6e6ce7af1ecb1fb70933bc8135b02aa72fdfd10d1bad531ce215e53aa  gflags-46f73f88b18aee341538c0dfc22b1710a6abedef.tar.gz
32453e59c3d176bc31b0a55d30667f528bee19979728d8cdeb6fd8fe7143a3b9d0bf15f36ca31b2204570164a0c234702b6f5b4201061760e4edcaed664715f6  ade-cbe2db61a659c2cc304c3837406f95c39dfa938e.tar.gz
130a7a751fb40c55309c5b41ca8d56540e5533d922a811eb01991ac66064c37e52c00bc303d34ccb4eb3847ee8fbcf0e39bd71d05b8c02e8490f31c459c95175  googletest-9bd163b993459b2ca6ba2dc508577bbc8774c851.tar.gz
b099fc26d3ea36ebfe8a8f8c93a7cf2d6c51196a71752c316813dd671936367bbe82aaee0b7cbc761e9d671909d53ff8b77c32f1428cb7ac5087a489f5ff2b8a  oneDNN-2706f56ebab54415be48add2751072065f4b52ab.tar.gz
b6347720e08746a4fdbb6e09691644597c900a6c440283baac6b1008399c78c5c34157dbfdb21476a71a7c4fd47c2912c7a7629076761d1bcb26d2a34ba73ba8  cmake-config.patch
9a93c41120189166bfcd5986954de812ebb16e429711b3f637907d2abf79dab75aac55eb1ee04ccbcee06e47709c3dcdd22b56aef89e64bb82b30fa10ca3dfd2  pessimizing-move.patch
d02374561f469f5818a9c76e697d68a84550c191459a2b4f9c7085f5b5e39eb6767a143e281dc0952a950cc7de12e11dd4a4602082f596553e2755c20a7a4867  deprecated-copy.patch
9744e36e5f75d0b39e2862eb2fd6bd5cededda120775e2bd47a039e39f3a9594a6d900d5d060117bdfa1f733ecd08691aa709841f3c89d258b005fd5a9fdfb84  cmake-dont-force-build-type.patch
23b051129678fd790de975f9fdbd0fd7ffdc0dd4de772b7883ffd90bc670eb62292e561b0db72468e8e142701cf3befcefeb2d17020e8eca10ffefcb841b136b  include-stdexcept.patch
0adfa161396ca4788b7b94d5a3b075aace5b833e252599176343ba33116b254c6cd884cb54319b73a35ce38da3930f1b75f453d8d89c127582053167919087c9  fix-missing-include.patch
"
